package console

import (
	"github.com/robfig/cron/v3"
	"log"
	"zin/app/console/command/demo"
	"zin/framework"
	"zin/framework/cobra"
	"zin/framework/command"
)

// RunCommand is command
func RunCommand(container framework.Container) error {
	var rootCmd = &cobra.Command{
		Use:   "zin",
		Short: "./zin app start 启动服务",
		RunE: func(cmd *cobra.Command, args []string) error {
			//打印帮助指令
			cmd.InitDefaultHelpFlag()
			return cmd.Help()
		},
		CompletionOptions: cobra.CompletionOptions{DisableDefaultCmd: true},
	}

	//给cobra设置container
	rootCmd.SetContainer(container)

	//运行核心命令
	command.AddKernelCommands(rootCmd)

	// 绑定业务的命令
	AddAppCommand(rootCmd)

	return rootCmd.Execute()
}

// AddAppCommand 绑定业务的命令
func AddAppCommand(rootCmd *cobra.Command) {
	//  demo 例子
	rootCmd.AddCommand(demo.InitFoo())

	rootCmd.Cron = cron.New(cron.WithParser(cron.NewParser(cron.SecondOptional | cron.Minute | cron.Hour | cron.Dom | cron.Month | cron.Dow | cron.Descriptor)))

	rootCmd.AddCronFunc("*/1 * * * *", "test cron", "print log", func(i ...interface{}) {
		log.Println("cron running:", i)
	})

	// 每秒调用一次Foo命令
	rootCmd.AddCronCommand("* * * * * *", demo.FooCommand)
}
